package luo.hui.jiang.binarysearchoecursion;

/**
 * @author 罗惠江
 * @version 1.0
 * @email 1511619294@qq.com
 * @date 2020/8/18 22:49
 */
public class BinarySearchNoRecur {

	public static void main(String[] args) {
		int[] arr = {1, 3, 10, 11, 67, 100};
		int index = binarySearch(arr, 100);
		System.out.println(index);
	}

	//非递归的实现
	public static int binarySearch(int[] arr, int target) {
		int left = 0;
		int right = arr.length - 1;
		while (left <= right) {
			int mid = (left + right) / 2;
			if (arr[mid] == target) {
				return mid;
			} else if (arr[mid] > target) {
				right = mid - 1;
			} else {
				left = mid + 1;
			}
		}
		return -1;
	}
}
